package org.jeecg.modules.api.wlhy.complaint;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.wlhy.complaint.entity.HyComplaint;
import org.jeecg.modules.wlhy.complaint.service.IHyComplaintService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description: APP投诉
 * @author: luruirui
 * @date: 2021年05月07日 17:21
 */
@Api(tags="APP投诉")
@RestController
@RequestMapping("/api/wlhy/complaint")
@Slf4j
public class ApiComplaintController {
    @Autowired
    private IHyCustomerService hyCustomerService;
    @Autowired
    private IHyDriverService hyDriverService;
    @Autowired
    private IHyShippingNoteService hyShippingNoteService;
    @Autowired
    private IHyComplaintService hyComplaintService;


    @ApiOperation(value="APP投诉-发起的投诉", notes="APP投诉-发起的投诉")
    @GetMapping(value = "/listSend")
    public Result<?> queryPageListSend(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper <HyComplaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("complainant", getIdData());
        queryWrapper.orderByDesc("create_time");
        Page<HyComplaint> page = new Page<HyComplaint>(pageNo, pageSize);
        IPage<HyComplaint> pageList = hyComplaintService.page(page, queryWrapper);
        //检测被投诉人举证状态
        checkProofEndTime(pageList.getRecords());
        return Result.OK(pageList);
    }
    @ApiOperation(value="APP投诉-收到的投诉", notes="APP投诉-收到的投诉")
    @GetMapping(value = "/listReceive")
    public Result<?> queryPageListReceive(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
//        QueryWrapper<HyComplaint> queryWrapper = QueryGenerator.initQueryWrapper(new HyComplaint(), req.getParameterMap());
        QueryWrapper<HyComplaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("respondent", getIdData());
        queryWrapper.orderByDesc("create_time");
        Page<HyComplaint> page = new Page<HyComplaint>(pageNo, pageSize);
        IPage<HyComplaint> pageList = hyComplaintService.page(page, queryWrapper);
        //检测被投诉人举证状态
        checkProofEndTime(pageList.getRecords());
        return Result.OK(pageList);
    }

    //获取id
    private String getIdData(){
        //拿到user数据
        LoginUser loginUser = UserUtil.getLoginUser();
        String userId = loginUser.getId();
        String userType = loginUser.getUType();

        // 2货主 4司机
        if(userType.equals("2")){
            QueryWrapper<HyCustomer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            HyCustomer customer = hyCustomerService.getOne(queryWrapper);
            if(customer == null) return "";
            return customer.getId();
        }
        if(userType.equals("4")){
            QueryWrapper<HyDriver> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            HyDriver driver = hyDriverService.getOne(queryWrapper);
            if(driver == null) return "";
            return driver.getId();
        }

//        return Result.error("当前投诉人类型不是货主或司机：" + userType);
        return "";
    }

    //检测举证时间是否到期
    private void checkProofEndTime(List<HyComplaint> complaintList){
        for (HyComplaint complaint : complaintList) {
            if(!complaint.getComplaintStatus().equals("2")) continue;
            long start = complaint.getCheckTime().getTime();//开始时间
            long now = new Date().getTime();//当前时间
            int timeLen = 3 * 24 * 60 * 60;//时长
            long leftTime = timeLen - (now - start) / 1000;//剩余时间
            //无剩余时间
            if(leftTime <= 0){
                //举证状态
                hyComplaintService.proofEnd(complaint);
//                log.info("queryPageListSend: " + complaint.getComplaintNumber() + "设置为待处理！");
            }
        }
    }

    /**
     *   创建投诉
     *
     * @param hyComplaint
     * @return
     */
    @AutoLog(value = "APP投诉-创建投诉")
    @ApiOperation(value="APP投诉-创建投诉", notes="APP投诉-创建投诉")
    @PostMapping(value = "/create")
    public Result<?> create(@RequestBody HyComplaint hyComplaint) {
        if(StringUtils.isEmpty(hyComplaint.getShippingNoteId())){
            return Result.error("没有运单号数据");
        }
        if(StringUtils.isEmpty(hyComplaint.getComplainantDesc())){
            return Result.error("请填写投诉描述");
        }
        if(StringUtils.isEmpty(hyComplaint.getComplaintType())){
            return Result.error("请选择投诉类型");
        }
        String shippingNoteId = hyComplaint.getShippingNoteId();
        HyShippingNote hyShippingNote = hyShippingNoteService.getById(shippingNoteId);
        if(hyShippingNote == null) return Result.error("当前运单不存在：" + shippingNoteId);
        // 设置关联货源id
        hyComplaint.setOrderId(hyShippingNote.getOrderId());
        // 拿到当前用户 设置被投诉人信息
        LoginUser loginUser = UserUtil.getLoginUser();
        String userType = loginUser.getUType();
        // 2货主 4司机
        if(userType.equals("2")){
            //货主投诉司机
            hyComplaint.setComplaintAt("1");
            String driverId = hyShippingNote.getDriverId();
            hyComplaint.setRespondent(driverId);
        }
        else if(userType.equals("4")){
            //司机投诉货主
            hyComplaint.setComplaintAt("2");
            String customerId = hyShippingNote.getCustomerId();
            hyComplaint.setRespondent(customerId);
        }else {
            return Result.error("当前投诉人类型不是货主或司机：" + userType);
        }
        return hyComplaintService.create(hyComplaint);
    }

    //举证
    @AutoLog(value = "APP投诉-举证")
    @ApiOperation(value="APP投诉-举证", notes="APP投诉-举证")
    @PutMapping(value = "/proof")
    public Result<?> proof(@RequestBody HyComplaint hyComplaint) {
        if(StringUtils.isEmpty(hyComplaint.getRespondentDesc())){
            return Result.error("请填写投诉描述");
        }
        // 举证描述、举证图片
        return hyComplaintService.proof(hyComplaint);
    }
    //满意度评价
    @AutoLog(value = "APP投诉-满意度评价")
    @ApiOperation(value="APP投诉-满意度评价", notes="APP投诉-满意度评价")
    @PutMapping(value = "/evaluate")
    public Result<?> evaluate(@RequestBody HyComplaint hyComplaint) {
        return hyComplaintService.evaluate(hyComplaint);
    }
}
